package com.sh.d1.gameserver;

import com.google.protobuf.Message;
import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 广播员
 */
public final class Broadcaster {
    /**
     * 信道字典
     *  key=gatewayServerId
     */
    static private final Map<Integer, Channel> _chMap = new ConcurrentHashMap<>();

    /**
     * 用户客户端id
     */
    static private final Map<String,UserClientId>  _userClientIdMap = new ConcurrentHashMap<>();
//    static private final Map<UserClientId, Channel> _tempMap = null;

    /**
     * 私有化类默认构造器
     */
    private Broadcaster() {
    }

    /**
     * 添加信道
     *
     * @param remoteSessionId 会话 Id
     * @param ch 客户端信道
     */
    static public void addChannel(int  gatewayServerId, int remoteSessionId, Channel ch) {
        if(remoteSessionId <= 0 ||
                gatewayServerId <= 0 ||
                ch == null) {
            return;
        }
        UserClientId newId=new UserClientId(gatewayServerId, remoteSessionId);
        _userClientIdMap.put(newId.getStrKey(),newId);
        _chMap.putIfAbsent(gatewayServerId, ch);
    }

    /**
     * 移除客户端id
     * @param gatewayServerId  服务器id
     * @param remoteSessionId   会话id
     */
    static public void removeUserClientId(int  gatewayServerId, int remoteSessionId) {
        if(gatewayServerId <=0 || remoteSessionId <= 0) {
            return;
        }
        UserClientId oldId=new UserClientId(gatewayServerId, remoteSessionId);
        _userClientIdMap.remove(oldId.getStrKey());
    }

    /**
     * 移除信道
     *
     * @param ch 客户端信道
     */
    static public void removeChannel(Channel ch) {
        if (null != ch) {
            _chMap.values().remove(ch);
        }
    }

    /**
     * 广播消息
     *
     * @param msgObj 消息对象
     */
    static public void broadcast(Object msgObj) {
        if (null == msgObj) {
            return;
        }


        for(UserClientId userClientId : _userClientIdMap.values()) {
            if(null==userClientId) {
                continue;
            }
            Channel gatewayServerChannel = _chMap.get(userClientId._gatewayServerId);
            if(null==gatewayServerChannel) {
                continue;
            }
            InternalServerMsg innerMsg = new InternalServerMsg();
            innerMsg.setGatewayServerId(userClientId._gatewayServerId);
            innerMsg.setRemoteSessionId(userClientId._remoteSessionId);
            innerMsg.setMsgObj((Message) msgObj);

            gatewayServerChannel.writeAndFlush(innerMsg);
        }
    }

    /**
     * 用户客户端id  网关id+sessionId的联合主键
     */

    static private class UserClientId{
        //网关服务器id
        private final int  _gatewayServerId;
        //远程会话id
        private final int  _remoteSessionId;

        /**
         * 参数构造器
         */
        public UserClientId(int gatewayServerId, int remoteSessionId) {
            _gatewayServerId = gatewayServerId;
            _remoteSessionId = remoteSessionId;
        }

        /**
         * 获取字符串关键字
         * @return  字符串关键字
         */
        public String getStrKey(){
            return _gatewayServerId + "/" + _remoteSessionId;
        }
    }
}
